home *** CD-ROM | disk | FTP | other *** search
- /*
- * compress.c
- *
- * Copyright (c) 1987 by Bear River Associates, Inc.
- */
-
-
- /* Primary Interface Files */
- #include "Types.h"
- #include "Resources.h"
- #include "Quickdraw.h"
- #include "Windows.h"
- #include "OSUtils.h"
-
- #include "ToolUtils.h"
- #include "TextEdit.h"
- #include "Controls.h"
-
- /* Application-specific Include files */
- #include "tifflib.h"
-
- struct CodeStr
- {
- Int16 runlength;
- Int16 codelength;
- UInt16 pattern;
- };
-
- #define entry(a, b, c, d) a,b,(d<<(16 - b))
-
- static struct CodeStr whiteTC[] =
- {
- entry( 0, 8, "0011 0101", 0x35),
- entry( 1, 6, "00 0111", 0x7),
- entry( 2, 4, "0111", 0x7),
- entry( 3, 4, "1000", 0x8),
- entry( 4, 4, "1011", 0xb),
- entry( 5, 4, "1100", 0xc),
- entry( 6, 4, "1110", 0xe),
- entry( 7, 4, "1111", 0xf),
- entry( 8, 5, "1 0011", 0x13),
- entry( 9, 5, "1 0100", 0x14),
- entry(10, 5, "0 0111", 0x7),
- entry(11, 5, "0 1000", 0x8),
- entry(12, 6, "00 1000", 0x8),
- entry(13, 6, "00 0011", 0x3),
- entry(14, 6, "11 0100", 0x34),
- entry(15, 6, "11 0101", 0x35),
- entry(16, 6, "10 1010", 0x2a),
- entry(17, 6, "10 1011", 0x2b),
- entry(18, 7, "010 0111", 0x27),
- entry(19, 7, "000 1100", 0xc),
- entry(20, 7, "000 1000", 0x8),
- entry(21, 7, "001 0111", 0x17),
- entry(22, 7, "000 0011", 0x3),
- entry(23, 7, "000 0100", 0x4),
- entry(24, 7, "010 1000", 0x28),
- entry(25, 7, "010 1011", 0x2b),
- entry(26, 7, "001 0011", 0x13),
- entry(27, 7, "010 0100", 0x24),
- entry(28, 7, "001 1000", 0x18),
- entry(29, 8, "0000 0010", 0x2),
- entry(30, 8, "0000 0011", 0x3),
- entry(31, 8, "0001 1010", 0x1a),
- entry(32, 8, "0001 1011", 0x1b),
- entry(33, 8, "0001 0010", 0x12),
- entry(34, 8, "0001 0011", 0x13),
- entry(35, 8, "0001 0100", 0x14),
- entry(36, 8, "0001 0101", 0x15),
- entry(37, 8, "0001 0110", 0x16),
- entry(38, 8, "0001 0111", 0x17),
- entry(39, 8, "0010 1000", 0x28),
- entry(40, 8, "0010 1001", 0x29),
- entry(41, 8, "0010 1010", 0x2a),
- entry(42, 8, "0010 1011", 0x2b),
- entry(43, 8, "0010 1100", 0x2c),
- entry(44, 8, "0010 1101", 0x2d),
- entry(45, 8, "0000 0100", 0x4),
- entry(46, 8, "0000 0101", 0x5),
- entry(47, 8, "0000 1010", 0xa),
- entry(48, 8, "0000 1011", 0xb),
- entry(49, 8, "0101 0010", 0x52),
- entry(50, 8, "0101 0011", 0x53),
- entry(51, 8, "0101 0100", 0x54),
- entry(52, 8, "0101 0101", 0x55),
- entry(53, 8, "0010 0100", 0x24),
- entry(54, 8, "0010 0101", 0x25),
- entry(55, 8, "0101 1000", 0x58),
- entry(56, 8, "0101 1001", 0x59),
- entry(57, 8, "0101 1010", 0x5a),
- entry(58, 8, "0101 1011", 0x5b),
- entry(59, 8, "0100 1010", 0x4a),
- entry(60, 8, "0100 1011", 0x4b),
- entry(61, 8, "0011 0010", 0x32),
- entry(62, 8, "0011 0011", 0x33),
- entry(63, 8, "0011 0100", 0x34)
- };
-
-
- static struct CodeStr whiteMUC[] =
- {
- entry( 64, 5, "1 1011", 0x1b),
- entry( 128, 5, "1 0010", 0x12),
- entry( 192, 6, "01 0111", 0x17),
- entry( 256, 7, "011 0111", 0x37),
- entry( 320, 8, "0011 0110", 0x36),
- entry( 384, 8, "0011 0111", 0x37),
- entry( 448, 8, "0110 0100", 0x64),
- entry( 512, 8, "0110 0101", 0x65),
- entry( 576, 8, "0110 1000", 0x68),
- entry( 640, 8, "0110 0111", 0x67),
- entry( 704, 9, "0 1100 1100", 0xcc),
- entry( 768, 9, "0 1100 1101", 0xcd),
- entry( 832, 9, "0 1101 0010", 0xd2),
- entry( 896, 9, "0 1101 0011", 0xd3),
- entry( 960, 9, "0 1101 0100", 0xd4),
- entry(1024, 9, "0 1101 0101", 0xd5),
- entry(1088, 9, "0 1101 0110", 0xd6),
- entry(1152, 9, "0 1101 0111", 0xd7),
- entry(1216, 9, "0 1101 1000", 0xd8),
- entry(1280, 9, "0 1101 1001", 0xd9),
- entry(1344, 9, "0 1101 1010", 0xda),
- entry(1408, 9, "0 1101 1011", 0xdb),
- entry(1472, 9, "0 1001 1000", 0x98),
- entry(1536, 9, "0 1001 1001", 0x99),
- entry(1600, 9, "0 1001 1010", 0x9a),
- entry(1664, 6, "01 1000", 0x18),
- entry(1728, 9, "0 1001 1011", 0x9b),
- entry(1792,11, "000 0000 1000", 0x8),
- entry(1856,11, "000 0000 1100", 0xc),
- entry(1920,11, "000 0000 1101", 0xd),
- entry(1984,12, "0000 0001 0010", 0x12),
- entry(2048,12, "0000 0001 0011", 0x13),
- entry(2112,12, "0000 0001 0100", 0x14),
- entry(2176,12, "0000 0001 0101", 0x15),
- entry(2240,12, "0000 0001 0110", 0x16),
- entry(2304,12, "0000 0001 0111", 0x17),
- entry(2368,12, "0000 0001 1100", 0x1c),
- entry(2432,12, "0000 0001 1101", 0x1d),
- entry(2496,12, "0000 0001 1110", 0x1e),
- entry(2560,12, "0000 0001 1111", 0x1f)
- };
-
- static struct CodeStr blackTC[] =
- {
- entry( 0,10, "00 0011 0111", 0x37),
- entry( 1, 3, "010", 0x2),
- entry( 2, 2, "11", 0x3),
- entry( 3, 2, "10", 0x2),
- entry( 4, 3, "011", 0x3),
- entry( 5, 4, "0011", 0x3),
- entry( 6, 4, "0010", 0x2),
- entry( 7, 5, "0 0011", 0x3),
- entry( 8, 6, "00 0101", 0x5),
- entry( 9, 6, "00 0100", 0x4),
- entry(10, 7, "000 0100", 0x4),
- entry(11, 7, "000 0101", 0x5),
- entry(12, 7, "000 0111", 0x7),
- entry(13, 8, "0000 0100", 0x4),
- entry(14, 8, "0000 0111", 0x7),
- entry(15, 9, "0 0001 1000", 0x18),
- entry(16,10, "00 0001 0111", 0x17),
- entry(17,10, "00 0001 1000", 0x18),
- entry(18,10, "00 0000 1000", 0x8),
- entry(19,11, "000 0110 0111", 0x67),
- entry(20,11, "000 0110 1000", 0x68),
- entry(21,11, "000 0110 1100", 0x6c),
- entry(22,11, "000 0011 0111", 0x37),
- entry(23,11, "000 0010 1000", 0x28),
- entry(24,11, "000 0001 0111", 0x17),
- entry(25,11, "000 0001 1000", 0x18),
- entry(26,12, "0000 1100 1010", 0xca),
- entry(27,12, "0000 1100 1011", 0xcb),
- entry(28,12, "0000 1100 1100", 0xcc),
- entry(29,12, "0000 1100 1101", 0xcd),
- entry(30,12, "0000 0110 1000", 0x68),
- entry(31,12, "0000 0110 1001", 0x69),
- entry(32,12, "0000 0110 1010", 0x6a),
- entry(33,12, "0000 0110 1011", 0x6b),
- entry(34,12, "0000 1101 0010", 0xd2),
- entry(35,12, "0000 1101 0011", 0xd3),
- entry(36,12, "0000 1101 0100", 0xd4),
- entry(37,12, "0000 1101 0101", 0xd5),
- entry(38,12, "0000 1101 0110", 0xd6),
- entry(39,12, "0000 1101 0111", 0xd7),
- entry(40,12, "0000 0110 1100", 0x6c),
- entry(41,12, "0000 0110 1101", 0x6d),
- entry(42,12, "0000 1101 1010", 0xda),
- entry(43,12, "0000 1101 1011", 0xdb),
- entry(44,12, "0000 0101 0100", 0x54),
- entry(45,12, "0000 0101 0101", 0x55),
- entry(46,12, "0000 0101 0110", 0x56),
- entry(47,12, "0000 0101 0111", 0x57),
- entry(48,12, "0000 0110 0100", 0x64),
- entry(49,12, "0000 0110 0101", 0x65),
- entry(50,12, "0000 0101 0010", 0x52),
- entry(51,12, "0000 0101 0011", 0x53),
- entry(52,12, "0000 0010 0100", 0x24),
- entry(53,12, "0000 0011 0111", 0x37),
- entry(54,12, "0000 0011 1000", 0x38),
- entry(55,12, "0000 0010 0111", 0x27),
- entry(56,12, "0000 0010 1000", 0x28),
- entry(57,12, "0000 0101 1000", 0x58),
- entry(58,12, "0000 0101 1001", 0x59),
- entry(59,12, "0000 0010 1011", 0x2b),
- entry(60,12, "0000 0010 1100", 0x2c),
- entry(61,12, "0000 0101 1010", 0x5a),
- entry(62,12, "0000 0110 0110", 0x66),
- entry(63,12, "0000 0110 0111", 0x67)
- };
-
- static struct CodeStr blackMUC[] =
- {
- entry( 64,10, "00 0000 1111", 0xf),
- entry( 128,12, "0000 1100 1000", 0xc8),
- entry( 192,12, "0000 1100 1001", 0xc9),
- entry( 256,12, "0000 0101 1011", 0x5b),
- entry( 320,12, "0000 0011 0011", 0x33),
- entry( 384,12, "0000 0011 0100", 0x34),
- entry( 448,12, "0000 0011 0101", 0x35),
- entry( 512,13, "0 0000 0110 1100", 0x6c),
- entry( 576,13, "0 0000 0110 1101", 0x6d),
- entry( 640,13, "0 0000 0100 1010", 0x4a),
- entry( 704,13, "0 0000 0100 1011", 0x4b),
- entry( 768,13, "0 0000 0100 1100", 0x4c),
- entry( 832,13, "0 0000 0100 1101", 0x4d),
- entry( 896,13, "0 0000 0111 0010", 0x72),
- entry( 960,13, "0 0000 0111 0011", 0x73),
- entry(1024,13, "0 0000 0111 0100", 0x74),
- entry(1088,13, "0 0000 0111 0101", 0x75),
- entry(1152,13, "0 0000 0111 0110", 0x76),
- entry(1216,13, "0 0000 0111 0111", 0x77),
- entry(1280,13, "0 0000 0101 0010", 0x52),
- entry(1344,13, "0 0000 0101 0011", 0x53),
- entry(1408,13, "0 0000 0101 0100", 0x54),
- entry(1472,13, "0 0000 0101 0101", 0x55),
- entry(1536,13, "0 0000 0101 1010", 0x5a),
- entry(1600,13, "0 0000 0101 1011", 0x5b),
- entry(1664,13, "0 0000 0110 0100", 0x64),
- entry(1728,13, "0 0000 0110 0101", 0x65),
- entry(1792,11, "000 0000 1000", 0x8),
- entry(1856,11, "000 0000 1100", 0xc),
- entry(1920,11, "000 0000 1101", 0xd),
- entry(1984,12, "0000 0001 0010", 0x12),
- entry(2048,12, "0000 0001 0011", 0x13),
- entry(2112,12, "0000 0001 0100", 0x14),
- entry(2176,12, "0000 0001 0101", 0x15),
- entry(2240,12, "0000 0001 0110", 0x16),
- entry(2304,12, "0000 0001 0111", 0x17),
- entry(2368,12, "0000 0001 1100", 0x1c),
- entry(2432,12, "0000 0001 1101", 0x1d),
- entry(2496,12, "0000 0001 1110", 0x1e),
- entry(2560,12, "0000 0001 1111", 0x1f)
- };
-
- /* ------------------------------------------------------------------- */
-
- OSErr HEncode(srcPtr, dstPtr, imageWidth)
- Ptr *srcPtr;
- Ptr *dstPtr;
- Int16 imageWidth;
- {
- Int16 srcBit;
- Int16 dstBit;
- Int8 srcByte;
- UInt8 dstByte;
- Int16 srcBitCount;
- Int16 dstBitCount;
- Boolean whiteRun;
- Int16 index;
- Int16 runLength;
- Int16 bytesLeft;
- Int16 leftToAdd;
- OSErr error;
- Int16 codelength;
- UInt16 pattern;
- UInt16 tempWord;
- Boolean putRun;
-
- leftToAdd = imageWidth;
- bytesLeft = (imageWidth + 7) / 8;
- srcBit = 0;
- dstBit = 0;
- srcBitCount = 0;
- dstBitCount = 0;
- dstByte = 0;
- whiteRun = TRUE;
- error = noErr;
- runLength = 0;
-
- while (leftToAdd > 0)
- {
- if (bytesLeft > 0 && srcBitCount == 0)
- { /* read a new byte */
- srcByte = *(*srcPtr)++;
- srcBitCount = 8;
- bytesLeft--;
- }
-
- /* if bytesLeft == 0 then no more bytes are available and
- srcBitCount will remain at zero. This will result in execution
- falling through to the else clause below */
-
- if (srcBitCount == 8 && srcByte == (whiteRun ? 0x0000 : 0xffff))
- { /* whole byte is continuation of a run */
- runLength += 8;
- srcBitCount = 0;
- }
- else if (srcBitCount > 0 &&
- ((srcByte >= 0 && whiteRun) || (srcByte < 0 && !whiteRun)))
- { /* next bit is continuation of a run */
- srcByte = srcByte << 1;
- srcBitCount--;
- runLength++;
- }
- else
- { /* end of run */
- if (runLength > leftToAdd) runLength = leftToAdd;
- leftToAdd -= runLength;
- putRun = FALSE;
- while (!putRun)
- {
- if (runLength > 2560)
- {
- codelength = whiteRun ?
- whiteMUC[2560 / 64 - 1].codelength :
- blackMUC[2560 / 64 - 1].codelength;
- pattern = whiteRun ?
- whiteMUC[2560 / 64 - 1].pattern :
- blackMUC[2560 / 64 - 1].pattern;
- runLength -= 2560;
- }
- else if (runLength >= 64)
- {
- codelength = whiteRun ?
- whiteMUC[runLength / 64 - 1].codelength :
- blackMUC[runLength / 64 - 1].codelength;
- pattern = whiteRun ?
- whiteMUC[runLength / 64 - 1].pattern :
- blackMUC[runLength / 64 - 1].pattern;
- runLength %= 64;
- }
- else
- {
- codelength = whiteRun ?
- whiteTC[runLength].codelength :
- blackTC[runLength].codelength;
- pattern = whiteRun ?
- whiteTC[runLength].pattern :
- blackTC[runLength].pattern;
- runLength = 0;
- putRun = TRUE;
- }
-
- while (codelength > 0)
- {
- tempWord = dstByte;
- tempWord |= (pattern >> (8 + dstBitCount));
- dstByte = tempWord;
- if (codelength <= 8 - dstBitCount)
- {
- dstBitCount += codelength;
- codelength = 0;
- }
- else
- {
- codelength -= (8 - dstBitCount);
- pattern = pattern << (8 - dstBitCount);
- dstBitCount = 8;
- }
- if (dstBitCount == 8)
- {
- *(*dstPtr)++ = dstByte;
- dstByte = 0;
- dstBitCount = 0;
- }
- }
- }
- whiteRun = !whiteRun;
- }
- }
-
- if (dstBitCount > 0)
- {
- *(*dstPtr)++ = dstByte;
- dstByte = 0;
- dstBitCount = 0;
- }
-
- return(error);
- }
-
-